package system

import (
	"context"
	"gf-assistant/internal/common"
	"gf-assistant/internal/common/factory"
	"gf-assistant/internal/system/parameter"
	"github.com/gogf/gf/v2/util/gconv"
	"strings"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/frame/g"
)

// Login 登录控制器类
var Login = LoginController{
	service: "system.LoginService",
}

type LoginController struct {
	service string
}

func (c *LoginController) Login(ctx context.Context, req *parameter.LoginReq) (res *parameter.LoginRes, err error) {
	msg, data, err := factory.Get(c.service).(*LoginService).Login(ctx, req.Username, req.Password)
	if !g.IsEmpty(msg) && strings.Compare(strings.ToLower(msg), "ok") != 0 {
		common.JsonExit(g.RequestFromCtx(ctx), gcode.CodeNil.Code(), msg, gconv.Uint(0))
		return
	}
	return data, err
}
